﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bird : MonoBehaviour
{
    public GameManager gameManager;


    private Rigidbody2D rigidbody2d;
    private Vector2 speed;
    private Vector2 maxspeed;
    private Vector2 grav;
    private Vector2 maxgrav;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();
        speed = Vector2.up * 6.5f;
        maxspeed = Vector2.up * 5;
        grav = Vector2.down * 0.3f;
        maxgrav = Vector2.down * 7;
    }
     
    // Update is called once per frame
    void Update()
    {
        
    }

    public void ResetPos()
    {
        transform.position = Vector3.zero;
    }
    public void Stop()
    {
        rigidbody2d.velocity = Vector2.zero;
    }

    public void Fly()
    {
        if (rigidbody2d.velocity.y <= maxspeed.y)
        {
            rigidbody2d.velocity += speed;
        }
    }

    public void Grav()
    {
        if (rigidbody2d.velocity.y >= maxgrav.y)
        {
            rigidbody2d.velocity += grav;
        }
    }


    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("stone"))
        {
            gameManager.PlayHitSound();
            gameManager.GameOver();
        }
        //else if (collision.gameObject.CompareTag("gold"))
        //{
        //    gameManager.PlayGoldSound();
        //    gameManager.Score += collision.gameObject.GetComponent<Gold>().Value;
        //    Destroy(collision.gameObject);
        //}
        else if (collision.gameObject.CompareTag("bomb"))
        {
            gameManager.PlayBoomSound();
            collision.gameObject.GetComponent<Bomb>().Boom();
            gameManager.GameOver();
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("gold"))
        {
            gameManager.PlayGoldSound();
            gameManager.Score += collision.gameObject.GetComponent<Gold>().Value;
            Destroy(collision.gameObject);
        }
    }
}
